﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CNUtility;
using System.Windows.Forms;

namespace CNPageEditor
{
    /// <summary>
    /// Shows a color picker which lets you choose the color for future drawing.
    /// </summary>
    class Action_ChooseColor : Action
    {
        #region Action methods

        /// <summary>
        /// Sets up the action.
        /// </summary>
        public override void initialize()
        {
            // We show a color picker dialog...
            ColorDialog dialog = new ColorDialog();

            // We show the custom color picker...
            dialog.FullOpen = true;
            CustomColors customColors = new CustomColors();
            try
            {
                customColors = UserPreferences.getPropertyBag().getObject<CustomColors>("DefaultPen.CustomColors");
                dialog.CustomColors = customColors.getColors();
            }
            catch (Exception)
            {
                // Most likely the custom colors were not in the config file.
                // We just show the dialog without custom colors in this case.
            }

            DialogResult dialogResult = dialog.ShowDialog();
            if (dialogResult != DialogResult.OK)
            {
                raiseActionCanceledEvent();
                return;
            }

            // We store the color...
            m_iNewColor = dialog.Color.ToArgb();
            m_iPreviousColor = UserPreferences.getPropertyBag().getInt("DefaultPen.Color");
            UserPreferences.getPropertyBag().setInt("DefaultPen.Color", m_iNewColor);

            // We store the custom colors...
            customColors.setColors(dialog.CustomColors);
            UserPreferences.getPropertyBag().setObject<CustomColors>("DefaultPen.CustomColors", customColors);

            // We complete the event...
            raiseActionCompletedEvent();
        }

		/// <summary>
		/// Called when the action becomes inactive.
		/// </summary>
		public override void deactivate()
		{
		}

        /// <summary>
        /// Returns the user-readable description of this action.
        /// </summary>
        public override string getDescription()
        {
            return "Choose color";
        }

        /// <summary>
        /// Returns whether we support undo. (We do for this action.)
        /// </summary>
        public override bool supportsUndo()
        {
            return true;
        }

        /// <summary>
        /// This action is not repeatable, i.e. we do not want to show the
        /// dialog again after the action has completed.
        /// </summary>
        public override bool isRepeatable()
        {
            return false;
        }

        /// <summary>
        /// Undoes the action.
        /// </summary>
        public override void undo()
        {
            // We set the default color back to the previous color...
            UserPreferences.getPropertyBag().setInt("DefaultPen.Color", m_iPreviousColor);
        }

        /// <summary>
        /// Redoes the action.
        /// </summary>
        public override void redo()
        {
            UserPreferences.getPropertyBag().setInt("DefaultPen.Color", m_iNewColor);
        }

        // The touch events do not do anything, as this action just shows a dialog.
        public override void onTouchDown(BobcatMultiTouch.TouchArgs touchArgs)
        {
        }
        public override void onTouchMove(BobcatMultiTouch.TouchArgs touchArgs)
        {
        }
        public override void onTouchUp(BobcatMultiTouch.TouchArgs touchArgs)
        {
        }

        #endregion

        #region Private data

        /// <summary>
        /// A private class that holds the collection of custom colors from
        /// the color dialog. Having it in an object with get/set properties
        /// makes it easier to store in the UserPreferences property-bag.
        /// </summary>
        private class CustomColors
        {
            public int Color0 { get; set; }
            public int Color1 { get; set; }
            public int Color2 { get; set; }
            public int Color3 { get; set; }
            public int Color4 { get; set; }
            public int Color5 { get; set; }
            public int Color6 { get; set; }
            public int Color7 { get; set; }
            public int Color8 { get; set; }
            public int Color9 { get; set; }
            public int Color10 { get; set; }
            public int Color11 { get; set; }
            public int Color12 { get; set; }
            public int Color13 { get; set; }
            public int Color14 { get; set; }
            public int Color15 { get; set; }

            public void setColors(int[] colors)
            {
                Color0 = colors[0];
                Color1 = colors[1];
                Color2 = colors[2];
                Color3 = colors[3];
                Color4 = colors[4];
                Color5 = colors[5];
                Color6 = colors[6];
                Color7 = colors[7];
                Color8 = colors[8];
                Color9 = colors[9];
                Color10 = colors[10];
                Color11 = colors[11];
                Color12 = colors[12];
                Color13 = colors[13];
                Color14 = colors[14];
                Color15 = colors[15];
            }

            public int[] getColors()
            {
                int[] colors = new int[16];
                colors[0] = Color0;
                colors[1] = Color1;
                colors[2] = Color2;
                colors[3] = Color3;
                colors[4] = Color4;
                colors[5] = Color5;
                colors[6] = Color6;
                colors[7] = Color7;
                colors[8] = Color8;
                colors[9] = Color9;
                colors[10] = Color10;
                colors[11] = Color11;
                colors[12] = Color12;
                colors[13] = Color13;
                colors[14] = Color14;
                colors[15] = Color15;
                return colors;
            }
        }

        private int m_iPreviousColor = 0; // Stored so that we can undo the action.
        private int m_iNewColor = 0;

        #endregion
    }
}
